package com.cnap.multiclouldmgmt.controller;

import com.cnap.multiclouldmgmt.model.CommonResult;
import com.cnap.multiclouldmgmt.model.RepByPage;
import com.cnap.multiclouldmgmt.model.RepByPageWithDate;
import com.cnap.multiclouldmgmt.model.k8sModels.K8sClusterModel;
import com.cnap.multiclouldmgmt.model.log.OperationTypeEnum;
import com.cnap.multiclouldmgmt.model.privatecloud.PrivateVmCreateModel;
import com.cnap.multiclouldmgmt.service.K8sClusterService;
import com.cnap.multiclouldmgmt.service.MultiCloudMgmtService;
import com.cnap.multiclouldmgmt.service.OperationLogService;
import com.cnap.multiclouldmgmt.service.VmResourceService;
import com.cnap.multiclouldmgmt.util.CnapCloudMgmtConstants;
import com.cnap.multiclouldmgmt.util.DateUtils;
import com.cnap.multiclouldmgmt.util.FileUtils;
import com.cnap.multiclouldmgmt.util.TokenUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 混合云网管理-混合云网管理
 * 用于集群注册，删除等操作
 * @author dyq
 */
@Api(tags ="混合云网管理-混合云网管理对外API")
@RequestMapping(path = "/cnap/multicloudmgmtApi/v1/multiCloud")
@RestController
@ResponseBody
public class MultiCloudMgmtApiController {

    private final static Logger LOGGER = LoggerFactory.getLogger(MultiCloudMgmtApiController.class);

    @Autowired
    private MultiCloudMgmtService multiCloudMgmtService;


    @Autowired
    private VmResourceService vmResourceService;
    /**
     * 根据条件获取集群列表
     * @param cloudVendor
     * @param k8sClusterName
     * @param pageSize
     * @param pageNo
     * @return
     */
    @ApiOperation(value = "11-1 根据条件获取集群列表",httpMethod = "GET",notes = "入参云供应商cloudVendor，选择集群的名称k8sClusterName")
    @RequestMapping(path = "/getK8sClusterInfos",method = RequestMethod.GET)
    public CommonResult<RepByPageWithDate<K8sClusterModel>> getK8sClusterInfos(String cloudVendor, String k8sClusterName, int pageSize, int pageNo,
                                                                               String k8sStatus) {

        try {
            String createUser = null;
            RepByPage<K8sClusterModel> result =
                    multiCloudMgmtService.getK8sClusterInfos(cloudVendor,k8sClusterName,pageSize,pageNo,createUser);

            RepByPageWithDate<K8sClusterModel> resultWithDate = new RepByPageWithDate<K8sClusterModel>();
            Date recentDate = null;
            List<K8sClusterModel> k8sClusterModelsWithStatus = new ArrayList<>();

            if (result.getTotalNum() > 0){

                for (K8sClusterModel k8sClusterModel:result.getResult()){

                    if (recentDate == null){
                        recentDate = k8sClusterModel.getResfeshDate();
                    }else{

                        if (k8sClusterModel.getResfeshDate() != null){
                            if (k8sClusterModel.getResfeshDate().getTime() > recentDate.getTime()){
                                recentDate = k8sClusterModel.getResfeshDate();
                            }
                        }
                    }

                    if (StringUtils.isNotEmpty(k8sStatus)){

                        if (StringUtils.equalsIgnoreCase(k8sClusterModel.getStatus(),k8sStatus)){
                            k8sClusterModelsWithStatus.add(k8sClusterModel);
                        }

                    }
                }
            }

            if (recentDate == null){
                resultWithDate.setSyncTime(DateUtils.changeDateFormatYYYYMMDDHHMMSS(new Date()));
            }else {
                resultWithDate.setSyncTime(DateUtils.changeDateFormatYYYYMMDDHHMMSS(recentDate));

            }
            resultWithDate.setResult(result.getResult());
            if (StringUtils.isNotEmpty(k8sStatus)){
                resultWithDate.setResult(k8sClusterModelsWithStatus);
            }
            resultWithDate.setPageNo(result.getPageNo());
            resultWithDate.setTotalNum(result.getTotalNum());
            resultWithDate.setPageSize(result.getPageSize());
            resultWithDate.setOtherInfo(result.getOtherInfo());

            //根据条件获取集群列表
            return CommonResult.success(resultWithDate);

        }catch (Exception e){

            LOGGER.error("MultiCloudMgmtController getK8sClusterInfos error pageNo={}",pageNo,e);

            return CommonResult.failed(e.getMessage());
        }
    }

    /**
     * 注册一个集群通过token
     * @param k8sClusterModel
     * @return
     */
    @ApiOperation(value = "11-2-1 注册一个集群",httpMethod = "POST", notes = "注册一个集群")
    @RequestMapping(path = "/addK8sClusterByToken",method = RequestMethod.POST)
    public CommonResult<Void> addK8sClusterByToken(@RequestBody K8sClusterModel k8sClusterModel ) {
        try {

                //通过token注册集群
            multiCloudMgmtService.addK8sCluster(k8sClusterModel);

            return CommonResult.successMsg("addK8sClusterInfo success");

        }catch (Exception e){

            LOGGER.error("MultiCloudMgmtController addK8sClusterByToken error k8sClusterIp={}",k8sClusterModel.getK8sClusterIp(),e);
            return CommonResult.failed(e.getMessage());
        }
    }
}
